/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.execute.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.peripheralinterface.xzzf.service.IXzzfInterfaceService;
import com.icinfo.cloud.provider.punish.common.caseregister.service.IRegisterCommonService;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteHandlePersonRecordDto;
import com.icinfo.cloud.provider.punish.execute.mapper.ExecuteHandlePersonRecordMapper;
import com.icinfo.cloud.provider.punish.execute.model.ExecuteHandlePersonRecord;
import com.icinfo.cloud.provider.punish.execute.service.IExecuteHandlePersonRecordService;
import com.icinfo.cloud.provider.punish.execute.vo.ExecuteHandlePersonRecordVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述: 案源办案人员记录表-TRJ execute_handle_person_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月26日
 */
@Service
public class ExecuteHandlePersonRecordServiceImpl extends MyBatisServiceSupport implements IExecuteHandlePersonRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ExecuteHandlePersonRecordServiceImpl.class);

    @Autowired
    private ExecuteHandlePersonRecordMapper executeHandlePersonRecordMapper;

    @Resource
    private IXzzfInterfaceService xzzfInterfaceService;

    @Resource
    private IRegisterCommonService commonService;

    @Value("${isCheckHandleSign}")
    private String isCheckHandleSign;

    @Override
    public void saveExecuteHandlePersonRecord(ExecuteHandlePersonRecordDto dto) {
        String handlePersonId=dto.getHandlePersonId();
        if (StringUtils.isNotBlank(handlePersonId)) {
            if (ObjectUtil.isEmpty(xzzfInterfaceService.getLawEnforcementCertWithUserId(handlePersonId))) {
                throw new BusinessException("办案人员" + dto.getHandlePersonName() + "的执法证为空!");
            }
            dto.setLawEnforcementCert(xzzfInterfaceService.getLawEnforcementCertWithUserId(handlePersonId));
        }
        //  签名校验
        String handlePersonSignPic = commonService.getHandleSignData(handlePersonId);
        if ("true".equals(isCheckHandleSign)) {
            if (ObjectUtil.isEmpty(handlePersonSignPic)) {
                throw new BusinessException("办案人员" + dto.getHandlePersonName() + "的签名为空!");
            }
        }
        dto.setHandleSignPic(handlePersonSignPic);
        dto.setId(StringUtils.uuid());
        executeHandlePersonRecordMapper.insertSelective(dto);
    }

    @Override
    public List<ExecuteHandlePersonRecord> getExecuteHandlePersonRecordList(String executeId) {
        Example example=new Example(ExecuteHandlePersonRecord.class);
        example.createCriteria().andEqualTo("executeId",executeId);
        List<ExecuteHandlePersonRecord> executeIdHandlePersonRecords=executeHandlePersonRecordMapper.selectByExample(example);
        return executeIdHandlePersonRecords;
    }
    /**
     * 描述：【V1.0.0】根据案件id获取办案人员信息记录
     *
     * @param caseId
     * @return
     * @author tianrunjia
     */
    @Override
    public List<ExecuteHandlePersonRecordVo> getCaseHandlePersonRecordListByExecuteId(String caseId) {
        if (StringUtils.isNotEmpty(caseId)) {
            Example queryExample = new Example(ExecuteHandlePersonRecord.class);
            queryExample.createCriteria().andEqualTo("executeId", caseId);
            queryExample.setOrderByClause("handle_person_type asc");
            List<ExecuteHandlePersonRecord> caseHandlePersonRecordList = executeHandlePersonRecordMapper.selectByExample(queryExample);
            if (caseHandlePersonRecordList.size() > 0) {
                List<ExecuteHandlePersonRecordVo> executeHandlePersonRecordVoList = new ArrayList<>();
                caseHandlePersonRecordList.forEach(e -> {
                    ExecuteHandlePersonRecordVo executeHandlePersonRecordVo = new ExecuteHandlePersonRecordVo();
                    BeanUtils.copyProperties(e, executeHandlePersonRecordVo);
                    executeHandlePersonRecordVoList.add(executeHandlePersonRecordVo);
                });
                return executeHandlePersonRecordVoList;
            } else {
                //throw new BusinessException("未匹配到办案人员记录");
                return null;
            }
        } else {
            throw new BusinessException("非法传值，案件id为空！");
        }
    }
}